type RGB = [number, number, number]

export default {
	// 十六进制颜色转 RGB 颜色
	HexToRgb(str: string): RGB {
		str = str.replace('#', '')
		if (str.length !== 6) throw new Error('Invalid hex color format')
		const hxs: RGB = str.match(/../g)!.map((hex) => parseInt(hex, 16)) as RGB
		return hxs
	},

	// RGB 颜色转十六进制颜色
	RgbToHex(r: number, g: number, b: number): string {
		if ([r, g, b].some((value) => value < 0 || value > 255)) {
			throw new Error('RGB values must be between 0 and 255')
		}
		const hexs = [r, g, b].map((value) => value.toString(16).padStart(2, '0'))
		return '#' + hexs.join('')
	},

	// 加深颜色
	darken(color: string, level: number): string {
		const rgbc = this.HexToRgb(color)
		const darkened: RGB = rgbc.map((value) => Math.floor(value * (1 - level))) as RGB
		return this.RgbToHex(...darkened)
	},

	// 变淡颜色
	lighten(color: string, level: number): string {
		const rgbc = this.HexToRgb(color)
		const lightened: RGB = rgbc.map((value) => Math.floor((255 - value) * level + value)) as RGB
		return this.RgbToHex(...lightened)
	}
}
